package com.yigerende.controller;

import com.yigerende.feign.AccountFeign;
import com.yigerende.pojo.Account;
import com.yigerende.pojo.TUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("account")
public class AccountController {

    @Autowired
    private AccountFeign accountFeign;

    //进入登陆页面
    @GetMapping("/logout")
    public String logout(HttpSession session){
        //每次进入登陆页面清空session
        session.invalidate();
        return "login";
    }

    //进入首页
    @GetMapping("/index/sy")
    public String index(){
        return "main";
    }

    @PostMapping("/login")
    public String login(Model model, @RequestParam("username") String username, @RequestParam("password") String password, @RequestParam("type") String type,HttpSession session){
        //这里只管传递数据，判断是管理员还是用户在服务提供者那边已经判断
        Account account = accountFeign.login(username, password, type);
        String target = null;
        //账号或者密码错误
        if(account == null){
            model.addAttribute("msg","账号或者密码错误");
            target = "login";
        }else{
            if(type.equals("user")){
                session.setAttribute("user",account);
                target = "main";
            }
            if(type.equals("admin")){
                session.setAttribute("admin",account);
                target = "main";
            }
        }
        return target;
    }


}
